package com.ziytek.web.citizen.util;

import com.ziytek.web.citizen.constant.ServerConstant;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date;

/**
 * @author fenghx
 * @version v1.0
 * @className DateTimeUtils
 * @description 功能描述
 * @date 2023/7/11 14:17
 **/
@Slf4j
public class DateTimeUtils {

    public static Boolean isToday(String dataTime) {
        Date date = DateTimeUtils.toDate(dataTime, null);
        if (null == date) {
            return false;
        }

        LocalDate dbDateInLocal = date.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDate();
        return LocalDate.now().equals(dbDateInLocal);
    }

    /**
     * 字符串时间转时间戳
     *
     * @param dateStr 时间字符串
     * @param isMin   是否是一天最小时间
     * @return 时间戳数值
     */
    public static Long getTimeLong(String dateStr, boolean isMin) {
        if (StringUtil.isNotEmpty(dateStr)) {
            try {
                return new SimpleDateFormat(ServerConstant.DATE_FORMAT).parse(dateStr).getTime();
            } catch (Exception e) {
                log.error("QueryRequest toDate error:" + e);
                throw new RuntimeException("QueryRequest toDate error:" + e);
            }
        }

        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MAX);
        if (isMin) {
            startOfDay = localDateTime.with(LocalTime.MIN);
        }

        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant()).getTime();
    }


    /**
     * 字符串时间转时间
     *
     * @param dateStr 时间字符串
     * @return 时间戳数值
     */
    public static String getTimeString(String dateStr, String dataFormat, String toDataFormat) {
        if (StringUtil.isNotEmpty(dateStr)) {
            try {
                if (StringUtil.isEmpty(dataFormat)) {
                    dataFormat = ServerConstant.DATE_FORMAT_TWO;
                }
                long time = new SimpleDateFormat(dataFormat).parse(dateStr)
                        .getTime();

                return toDate(time, toDataFormat);
            } catch (Exception e) {
                log.error("QueryRequest toDate error:" + e);
                throw new RuntimeException("QueryRequest toDate error:" + e);
            }
        }

        return null;
    }

    /**
     * 字符串时间转时间
     *
     * @param dateStr 时间字符串
     * @return 时间戳数值
     */
    public static Date toDate(String dateStr, String dataFormat) {
        if (StringUtil.isNotEmpty(dateStr)) {
            try {
                if (StringUtil.isEmpty(dataFormat)) {
                    dataFormat = ServerConstant.DATE_FORMAT;
                }
                return new SimpleDateFormat(dataFormat).parse(dateStr);
            } catch (Exception e) {
                log.error("QueryRequest toDate error:", e);
                throw new RuntimeException("QueryRequest toDate error:" + e);
            }
        }

        return null;
    }

    public static String toDate(Long time, String dataFormat) {
        try {
            dataFormat = StringUtil.isEmpty(dataFormat) ? "yyyy-MM-dd HH:mm:ss" : dataFormat;
            SimpleDateFormat sdf = new SimpleDateFormat(dataFormat);
            return sdf.format(time);
        } catch (Exception e) {
            return null;
        }
    }

    public static Integer toDateInt(Long time, String dataFormat) {
        try {
            dataFormat = StringUtil.isEmpty(dataFormat) ? "yyyyMMdd" : dataFormat;
            SimpleDateFormat sdf = new SimpleDateFormat(dataFormat);
            return Integer.parseInt(sdf.format(time));
        } catch (Exception e) {
            return 0;
        }
    }

    public static Long toDateLong(Long time, String dataFormat) {
        try {
            dataFormat = StringUtil.isEmpty(dataFormat) ? "yyyyMMddHHmmss" : dataFormat;
            SimpleDateFormat sdf = new SimpleDateFormat(dataFormat);
            return Long.parseLong(sdf.format(time));
        } catch (Exception e) {
            return null;
        }
    }

    public static String toDate(Long time) {
        return toDate(time, null);
    }

    public static Integer toDateInt(String dateTime) {
        if (StringUtil.isEmpty(dateTime)) {
            return -1;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(dateTime);
            if (date == null) {
                return -2;
            }

            return toDateInt(date.getTime(), null);
        } catch (Exception e) {
            log.error("toDateInt error:", e);
        }

        return -3;
    }
}
